<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>

    // 打印函数
    function print() {
        console.log('123')
    }

    // 节流:规定的时间里，只能执行1次
    function fun1(func, delay = 500) {
        let timer = null
        return function () {
            if (!timer) {
                timer = setTimeout(() => {
                    print()
                    timer = null;
                }, delay)
            }
        }
    }

    fun1(print, 500)

    // 防抖:高频触发相同的事件，但只执行最后一次
    function fun2(fn,delay = 10000){
        let timer = null;
        return function(){
            if(timer){
                timer = null
            }
            timer = setTimeout(fn,delay)
        }
    }

    fun2(print,5000);
</script>
</body>
</html>
